
I was just reading a nice tip by Peter Bromberg, in his blog entry <a href='http://www.eggheadcafe.com/tutorials/aspnet/8b53894c-a889-4914-8c46-122980cc44ae/simple-xor-encryption.aspx'>Simple XOR Encryption</a>.

In <a href='http://weblogs.java.net/blog/evanx/archive/2007/02/password_hash.html'>Password Hash</a> we used the following delegating class for base-64 encoding. 
<p>
<pre>public class Base64 {    

    public static String encode(byte[] bytes) {
        return new sun.misc.BASE64Encoder().encode(bytes);
    }
    
    public static byte[] decode(String string) {       
        try {
            return new sun.misc.BASE64Decoder().decodeBuffer(string);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

</pre>
<p>
As an alternative to the XOR'ing approach, we could use this encoding as "encryption" against prying eyes if nothing else, as follows. 

<pre>
    public static void main(String[] args) {
        String parameters = "username=evanx;password=mypassword";
        String encryptedParameters = Base64.encode(parameters);
        System.out.println(encryptedParameters);
        parameters = new String(Base64.decode(encryptedParameters));
        System.out.println(parameters);
    }
</pre>

which outputs the following

<pre class='javaStyle'>
dXNlcm5hbWU9ZXZhbng7cGFzc3dvcmQ9bXlwYXNzd29yZA==
username=evanx;password=mypassword
</pre>

Developers 1, Managers 0 ;)
 
<i>Update: this is not encryption, just obfuscation! See my <a href="http://weblogs.java.net/blog/evanx/archive/2007/04/encryptic.html">Encryptic</a> follow up.</i>